<?php
/**
 * Created by PhpStorm.
 * User: tri
 * Date: 8/10/2014
 * Time: 5:09 PM
 */

session_start();
error_reporting(1);
require_once('../../class_ajax_request/classAjax.php');
include_once('../../application/functions.php');
include_once('../../application/DataConfig.php');

if(isset($_POST)){
    $obj = new AjaxRequest();
    $idPost = $_POST['idpost'];
    $votersCount = count($obj->getVoter($idPost));
    $countReply = $obj->countReply($idPost);


    $obj      = new AjaxRequest();

    $voters = $obj->getVoter($idPost);
    $sexCount = $obj->getStatSexVoter($idPost);
    $ageCount = $obj->getStatAgeVoter($idPost);
    $sexCount['male'] = isset($sexCount['male'])?$sexCount['male']:0;
    $sexCount['female'] = isset($sexCount['female'])?$sexCount['female']:0;
    $datatext = '["male", '.$sexCount['male'].'],
     ["female", '.$sexCount['female'].']
';

//sex chart
    $html_sex = $obj->renderPieChart($datatext,$idPost,'staticsSexPost',array('title'=>'Gender','color2'=>'1'));
    //echo $html;
//birthChart
    $child = isset($ageCount['children'])?$ageCount['children']:0;
    $teen= isset($ageCount['teens'])?$ageCount['teens']:0;
    $youth= isset($ageCount['youth'])?$ageCount['youth']:0;
    $youngAdults = isset($ageCount['young-adults'])?$ageCount['young-adults']:0;
    $adults = isset($ageCount['adults'])?$ageCount['adults']:0;
    $senior = isset($ageCount['senior'])?$ageCount['senior']:0;
    $databirthtext  = ' ["Children", '.$child.'],
                    ["Teens", '.$teen.'],
                    ["Youth", '.$youth.'],
                    ["Young", '.$youngAdults.'],
                    ["Adults", '.$adults.'],
                    ["Senior", '.$senior.']';
    $html_age = $obj->renderPieChart($databirthtext,$idPost,'staticsBirthPost',array('title'=>'Age'));


    $html = '<div class="stat-col">
                <div class="col-lg-6">'.$html_sex.'</div>
                <div class="col-lg-6">'.$html_age.'</div>
            </div>';

    $voters = $obj->getVoter($idPost);
//echo '<pre>';
//print_r($voters);
//echo '</pre>';
    $html_voter = '<ul class="voter-list">';
    foreach($voters as $voter){
        $user_id = $voter['user_id'];
        $user_name = $voter['user_name'];
        $followBtnHtml = '';
        $checkFollow = $obj->checkFollow($_SESSION['authenticated'],$user_id);
        $key['followActive'] = isset($checkFollow[0]['status'])?$checkFollow[0]['status']:0;
 if(  isset( $_SESSION['authenticated'] ) && $_SESSION['authenticated'] != $user_id && $key['followActive'] == 0 ){
     $followBtnHtml = '<span data-username="'.$user_name.'" data-id="'._Function::randomString( 10, FALSE, TRUE, FALSE ).'-'.$user_id.'" data-follow="'.$_SESSION['LANG']['follow'].'" data-unfollow="'.$_SESSION['LANG']['unfollow'].'" data-following="'.$_SESSION['LANG']['following'].'" class="followBtn follow_button followBtnMin">'.$_SESSION['LANG']['follow'].'</span>';
 }

if(  isset( $_SESSION['authenticated'] ) && $_SESSION['authenticated'] != $user_id && $key['followActive'] == 1 ){
    $followBtnHtml = '<span data-username="'.$user_name.'" data-id="'._Function::randomString( 10, FALSE, TRUE, FALSE ).'-'.$user_id.'" data-follow="'.$_SESSION['LANG']['follow'].'" data-unfollow="'. $_SESSION['LANG']['unfollow'].'" data-following="'.$_SESSION['LANG']['following'].'" class="followBtn follow_button follow_active followBtnMin">'.$_SESSION['LANG']['following'].'</span>';
}

        $avatar = $voter['user_avatar'];
        $avatar = _Function::checkExistsAvatar($avatar,'48-48');
        //echo $avatar;

        $userLink = URL_BASE.$key['username'];
        $html_voter .= '<li>';
        $html_voter .= '<span class="paddingPost">
        <a href="'.$userLink.'" data-id="11" class="openModal" style="float:left; width:10%">
         			<img class="avatar_user" src="'.$avatar.'">
        </a>
        <span class="detail_grid" style="
    width: 89%;
    margin-top: 10px;
">
        <a href="'.$userLink.'"><span class="timestamp timeAgo voterRealName">'.$voter['user_real_name'].'</span></a>
        <a href="'.$userLink.'"><p class="voterUserName">'.$voter['user_name'].'</p></a>
        <p class="voterAnswerVal">Said: <span>'.$voter['user_answer_value'].'<span></p>'.$followBtnHtml.'
    </span>
    </span>';
        $html_voter .= '</li>';
    }
    $html_voter .= '</ul>';

    //replay
    $getReply       = $obj->getReply( $idPost );
    $countReply1     = count( $getReply );
    $replyArr = '';
    if( $countReply1 != 0 ) {
        $count = 1;
        for ( $u = 0; $u < $countReply1; ++$u ) {
            //<--- * User Verified * --->
            if( $getReply[$u]['type_account'] == 1 ) {
                $verified = ' <img class="verified_img" src="'.URL_BASE.'public/img/verified_min.png">';
            } else  {
                $verified = null;
            }
            //<--- * Delete Reply* --->
            if( $_SESSION['authenticated'] == $getReply[$u]['id'] ) {
                $removeReply = ' <i title="'.$_SESSION['LANG']['delete'].'" data="'.$getReply[$u]['idReply'].'" class="trash_ico_reply removeReply"></i>';
            } else {
                $removeReply = null;
            }
            $avatar = $getReply[$u]['avatar'];
            if(strstr($avatar,'facebook') == false){
                $avatar =   URL_BASE.'thumb/48-48-public/avatar/'.$getReply[$u]['avatar'];
                $status = get_headers($avatar);
                if($status[0] == 'HTTP/1.1 404 Not Found'){
                    $avatar =  URL_BASE."thumb/48-48-public/avatar/avatar.png";
                }
            }



            $replyArr .= "<span class='spanReply'>".$removeReply."<span class='paddingReply'>
			<a href='".URL_BASE.$getReply[$u]['username']."' class='openModal' data-id='".$getReply[$u]['id']."'>
				<img class='avatar_user' src='".$avatar."'>
			</a>
			 <span class='replyContainer'> <span class='timestamp timeAgo' data='".date('c', strtotime( $getReply[$u]['date'] ) )."'>
			 </span> <a href='".URL_BASE.$getReply[$u]['username']."' class='userR openModal' data-id='".$getReply[$u]['id']."'>".stripslashes( $getReply[$u]['name'] ).$verified."</a>
			 <strong class='usernameClass'>@".$getReply[$u]['username']."</strong> <p>"._Function::checkText( $getReply[$u]['reply'] )."</p>
			 </span></span> </span><!-- SPAN REPLY -->";
            $count++;
        }
    }

    //fav
    $htmlfav = '';
    $getFavorites   = $obj->getFavorites( $_postId );
    $countFavs      = count( $getFavorites );
    if( $countFavs != 0 ) {
        for ( $i = 0; $i < $countFavs; ++$i ) {
            $avatar = $getFavorites[$i]['avatar'];
            if(strstr($avatar,'facebook') == false){
                $avatar =   URL_BASE."thumb/48-48-public/avatar/".$getFavorites[$i]['avatar'];
            }
            $htmlfav .= '<a class="south openModal" data-id="'.$getFavorites[$i]['id'].'" title="@'.$getFavorites[$i]['username'].'" href="'.URL_BASE.$getFavorites[$i]['username'] .'"><img src="'.$avatar.'" /></a>';
        }
    }//<---
//    date('d/m/Y', strtotime( $getMedia[0]['date'] ) ) -

    $htmlFavReply = $htmlfav.'<!-- details - report -->
			<span class="details_report">
			   <span class="iDetails">
			     <a href="'.URL_BASE.$getMedia[0]['username'].'/status/'.$getMedia[0]['id'].'">'.$_SESSION['LANG']['details'].'</a>
			</span>
			</span>'.$replyArr;


    echo json_encode(
        array('count_reply'=>$countReply[0]['countReply'],
              'count_voter'=>$votersCount,
              'sex_chart'=>$html,
              'html_voter' => $html_voter,
              'html_reply' => $htmlFavReply));
}
